package com.acp.controller.uploadphotofile;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;


import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 解压压缩文件
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */


public class DecompressUtils {

    /**
     * 解压zip文件
     *
     * @param sourceFile,待解压的zip文件
     * @param toFolder,解压后的存放路径
     * @throws Exception
     **/
    public static void zipToFile(String sourceFile, String toFolder) throws Exception {
        String toDisk = toFolder;// 接收解压后的存放路径
        ZipFile zfile = new ZipFile(sourceFile, "utf-8");// 连接待解压文件
        Enumeration<?> zList = zfile.getEntries();// 得到zip包里的所有元素
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        while (zList.hasMoreElements()) {
            ze = (ZipEntry) zList.nextElement();
            if (ze.isDirectory()) {
                continue;
            }
            OutputStream outputStream = null;
            InputStream inputStream = null;
            try {
                outputStream = new BufferedOutputStream(new FileOutputStream(getRealFileName(toDisk, ze.getName())));
                inputStream = new BufferedInputStream(zfile.getInputStream(ze));
                int readLen = 0;
                while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {
                    outputStream.write(buf, 0, readLen);
                }
                inputStream.close();
                outputStream.close();
            } catch (Exception e) {
                throw new IOException("解压失败：" + e.toString());
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException ex) {


                    }
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
                inputStream = null;
                outputStream = null;
            }


        }
        zfile.close();
    }

    /**
     * 遍历文件
     * @param sourceFile
     * @param toFolder
     */

    public static void rarToFile(String sourceFile, String toFolder) {

        Archive archive = null;
        try{
            archive = new Archive(new File(sourceFile));
            if (archive != null) {
                archive.getMainHeader().print();
                FileHeader fileHeader = archive.nextFileHeader();
                while(fileHeader != null){
                    String fileName = fileHeader.getFileNameW().isEmpty()?fileHeader.getFileNameString():fileHeader.getFileNameW();
                    if (fileHeader.isDirectory()) {
                        File file = new File(toFolder+File.separator+fileName);
                        file.mkdirs();
                    }else {
                        File file = new File(toFolder+File.separator+fileName.trim());
                        try{
                            if (!file.exists()) {
                                if (!file.getParentFile().exists()) {
                                    file.getParentFile().mkdirs();
                                }
                                file.createNewFile();
                            }
                            FileOutputStream outputStream = new FileOutputStream(file);
                            archive.extractFile(fileHeader, outputStream);
                            outputStream.flush();
                            outputStream.close();
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                    fileHeader = archive.nextFileHeader();
                }
                archive.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }


    }

    /**
     *
     * 给定根目录，返回一个相对路径所对应的实际文件名
     * @param zippath ,指定根目录
     * @param absFileName ,相对路径名，来自于ZipEntry中的name
     * @return java.io.File 实际的文件
     *
     */
    private static File getRealFileName(String zippath, String absFileName) {
        System.out.println(absFileName);
        String[] dirs = absFileName.split("/", absFileName.length());
        File ret = new File(zippath);// 创建文件对象
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                ret = new File(ret, dirs[i]);
            }
        }
// 检测文件是否存在
        if (!ret.exists()) {
// 创建此抽象路径名指定的目录
            ret.mkdirs();
        }
// 根据 ret 抽象路径名和 child 路径名字符串创建一个新 File 实例
        ret = new File(ret, dirs[dirs.length - 1]);
        return ret;
    }

}
